package com.j2205.hongzhiyuan.service.impl;


import com.j2205.hongzhiyuan.mapper.PersonMapper;
import com.j2205.hongzhiyuan.pojo.Person;
import com.j2205.hongzhiyuan.service.PersonService;
import com.j2205.hongzhiyuan.util.MybatisUtil;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

public class PersonServiceImpl implements PersonService {
    @Override
    public boolean addPerson(Person person) {
        PersonMapper mapper = MybatisUtil.getSession().getMapper(PersonMapper.class);
//        if(person.getPtID() == 2) {
//
//        }
        int i = mapper.insertPerson(person);
        MybatisUtil.commit();
        if(i > -1) {
            return true;
        }
        MybatisUtil.clseAll();
        return false;
    }

    @Override
    public List<Person> findAllPerson() {
        PersonMapper mapper =  MybatisUtil.getSession().getMapper(PersonMapper.class);
        List<Person> people = mapper.selectAllPerson();
        if(people != null) {
            return people;
        }
        return null;
    }

    @Override
    public Person findPersonById(int id) {
        PersonMapper mapper =  MybatisUtil.getSession().getMapper(PersonMapper.class);
        Person person = mapper.selectPersonById(id);
        if(person != null) {
            return person;
        }
        return null;
    }

    @Override
    public boolean updPerson(Person person) {
        PersonMapper mapper = MybatisUtil.getSession().getMapper(PersonMapper.class);
        int i = mapper.updatePerson(person);
        MybatisUtil.commit();
        if(i > -1) {
            return true;
        }
        MybatisUtil.clseAll();
        return false;
    }

    @Override
    public boolean delPerson(int pid) {
        PersonMapper mapper =  MybatisUtil.getSession().getMapper(PersonMapper.class);
        int i = mapper.deletePerson(pid);
        MybatisUtil.commit();
        if(i > -1) {
            return true;
        }
        MybatisUtil.clseAll();
        return false;
    }
}
